package util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class OtherUtil {
    public static Map<String,Integer> getWordFrequency(String content,Map<String,Integer> wordAndFrequency){
        //todo 测试对比直接用ArrayList<Map.Entry<String,Integer>>完成效率
//        Map<String,Integer> wordAndFrequency =new HashMap<>(1024);
        //转为小写 后只留下英文字母
        content = content.toLowerCase().replaceAll("[^a-z ]+", "");
        String[] s = content.split(" ");
        for(String word :s){
            //不存在则添加进map
            wordAndFrequency.merge(word, 1, Integer::sum);
        }
//        //Map转为list排序
//        ArrayList<Map.Entry<String,Integer>> list = new ArrayList<>(wordAndFrequency.entrySet());
//        list.sort((o1, o2) -> o2.getValue() - o1.getValue());
        return wordAndFrequency;
    }
}
